package huawei.js2025;

import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

/**
 * 最大连续文件之和
 */
public class Question16 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int capacity = in.nextInt();
        in.nextLine();
        List<Integer> fileLink = Arrays.stream(in.nextLine().split(" ")).map(Integer::parseInt).collect(Collectors.toList());

        int left = 0, right = 0;
        int sum = 0;

        int maxCapacity = 0;
        while (left <= right && right < fileLink.size()) {
            if (sum < capacity) {
                sum += fileLink.get(right++);
            } else {
                sum -= fileLink.get(left++);
            }

            if (sum <= capacity) {
                maxCapacity = Math.max(maxCapacity, sum);
            }
        }

        System.out.println(maxCapacity);
    }
}
